/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.commons.scripting.functions.numbers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.zarubsys.commons.scripting.IScriptFunction;
import net.zarubsys.commons.scripting.ParsedStatement;
import net.zarubsys.commons.scripting.ScriptingEnvironment;
import net.zarubsys.commons.scripting.ScriptingException;
import net.zarubsys.commons.utils.StringUtils;

/**
 * Based on scripting language from Media Solutions Slovakia, a.s.
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class SumFunction implements IScriptFunction {

    public String getName() {
        return "sum";
    }

    public int getMinArgsNum() {
        return 0;
    }

    public int getMaxArgsNum() {
        return -1;
    }
    
    public Object execute(ScriptingEnvironment env, List<ParsedStatement> args) throws ScriptingException {
        List<Object> parsedArgs = new ArrayList<Object>(args.size());
        for (ParsedStatement st : args) {
        	parsedArgs.add(st.execute(env));
        }            
        Class<?> retClazz = Integer.class;
        Iterator<Object> iter = parsedArgs.iterator();
        while (iter.hasNext()) {
            Object val = iter.next();
            if (retClazz.equals(Integer.class)) {
                if (val instanceof Long) {
                    retClazz = Long.class;
                } else if (val instanceof Float) {
                    retClazz = Float.class;
                } else if (val instanceof Double) {
                    retClazz = Double.class;
                    break;
                }
            } else if (retClazz.equals(Long.class)) {
                if (val instanceof Float) {
                    retClazz = Float.class;
                } else if (val instanceof Double) {
                    retClazz = Double.class;
                    break;
                }
            } else if (retClazz.equals(Float.class)) {
                if (val instanceof Double) {
                    retClazz = Double.class;
                    break;
                }
            }
        }
        int sum_int = 0;
        long sum_long = 0;
        float sum_float = 0;
        double sum_double = 0;
        Iterator<Object> iter2 = parsedArgs.iterator();
        while (iter2.hasNext()) {
            Object val = iter2.next();
            if (val instanceof Integer) {
                sum_int += ((Integer) val).intValue();
            } else if (val instanceof Long) {
                sum_long += ((Long) val).longValue();
            } else if (val instanceof Float) {
                sum_float += ((Float) val).floatValue();
            } else if (val instanceof Double) {
                sum_double += ((Double) val).doubleValue();
            } else {
                sum_int += Integer.parseInt(StringUtils.toString(val));
            }
        }
        if (retClazz.equals(Integer.class)) {
            return Integer.valueOf(sum_int);
        } else if (retClazz.equals(Long.class)) {
            return Long.valueOf(sum_int + sum_long);
        } else if (retClazz.equals(Float.class)) {
            return Float.valueOf(sum_int + sum_long + sum_float);
        } else if (retClazz.equals(Double.class)) {
            return Double.valueOf(sum_int + sum_long + sum_float + sum_double);
        }
        throw new ScriptingException("INTERNAL ERROR: something went terribly wrong - unknown type :)))))", env.getCurrentScriptLocator());
    }

}
